<?php

namespace api\modules\v1\models;
use api\func\func;
use Yii;
use yii\db\Query;
use yii\web\HttpException;

class UcenterThird extends Ucenter
{

    public function getIndex(){
        $data = [];
        $data['article'] = $this->getArticle();
        return $data;
    }

    /*
     * 获取相关云号
     * @param string $uid
     * @param int $limit
     * @return array|null|\yii\db\ActiveRecord[]|static
     * @throws HttpException
     */
    public function about($limit=6){
        $user = $this->checkLoginAuth(); //获取第三方用户信息
        $my_user = $this->getTokenUserId(); //获取本账号信息

        $uid = $user['id'];
        $where = "user_id != {$uid}";//排除第三方用户id
        if($my_user != '') { //登录情况下
            $where .= " and user_id != {$my_user['id']}"; //排除本身用户id
            if($exists_fans = Fans::find()->select('to_user')->where(['user'=>$my_user['id']])->column()){ //排除本身已关注的用户id
                if(count($exists_fans) >0){
                    $exists_fans_uids = implode(',',$exists_fans);
                    $where .= " and user_id not in ({$exists_fans_uids})"; //排除本身已经关注用户
                }
            }
        }

        //匹配相关领域条件
        $renzheng_data  = ((new Query())->select("field,zilingyu")->from('{{%renzheng_app}}')->where(['user_id'=>$uid])->one());
        if($renzheng_data){
            foreach($renzheng_data as $key=>$value){
                if($value !='' && $value != 0){
                    $where .= " and {$key}= {$value}";
                }
            }
        }

        //根据前面条件 查出用户id
        $data  = ((new Query())
            ->select("DISTINCT `user_id`")
            ->from('{{%renzheng_app}} as a')
            ->innerJoin("{{%user_app}} as b","a.user_id = b.id")
            ->limit($limit)
            ->Where($where)
            ->orderBy('rand()')
            ->column());

        if(count($data) == 0 ){ //查询不到数据 返回空
            return [];
        }

        $user_ids = implode(',',$data);

        //根据id查出用户信息
        $user_data = User::find()->select('id,head_img,descript,nickname,phone')->where('id in ('.$user_ids.')')->asArray()->orderBy('rand()')->all();

        foreach($user_data as $key=>&$value){
            $value['nickname'] = $value['nickname'] != '' ? $value['nickname'] : func::replace_phone($value['phone']);
            $value['descript'] = $value['descript'] !='' ? $value['descript'] : Yii::$app->params['default_descript'];
            $value['head_img'] = $value['head_img'] != '' ? $value['head_img'] : Yii::$app->params['default_header'];
        }
        unset($value);
        return $user_data;
    }

    /*
     * 获取是否已经关注用户
     */
    public function getGz(){
        $user = $this->checkLoginAuth();
        $uid = $user['id'];
        $my_user = $this->getTokenUserId();
        if($my_user == '') return false;
        if(Fans::find()->where(['user'=>$my_user['id'],'to_user'=>$uid])->asArray()->one()){
            return true;
        }else{
            return false;
        }
    }

    public function checkLoginAuth(){
        if(Yii::$app->request->get('uid','') !=''){
            $uid = Yii::$app->request->get('uid','');
        }else{
            throw new HttpException(401,'未获取到用户ID',0);
        }
        $query = new \yii\db\Query();
        if($data = $query->from('{{%user_app}}')->select('id,username,collect_category,nickname,phone,score')->where(['id'=>$uid])->one()){
            return $data;
        }else{
            throw new HttpException(401,'该用户不存在',0);
        }
    }
}
